# -*- coding: utf-8 -*-
'''
Created on 2017年5月20日

@author: ZhuJiahui
'''

import os
import time
from snownlp import SnowNLP
from file_utils.file_writer import quick_write_1d_to_text

def get_sentiment_tags(read_filename, write_filename):
    
    sentiment_tags = []
    with open(read_filename, 'r', encoding='utf-8') as f:
        for each_line in f:
            if (len(each_line.strip()) > 100):
                sn = SnowNLP(each_line.strip()[:100])
            else:
                sn = SnowNLP(each_line.strip())
            if (sn.sentiments > 0.8):
                sentiment_tags.append('1')
            elif (sn.sentiments < 0.2):
                sentiment_tags.append('-1')
            else:
                sentiment_tags.append('0')
    
    quick_write_1d_to_text(write_filename, sentiment_tags)
    
def test_get_sentiment_tags():
    start = time.clock()
    now_directory = os.getcwd()
    root_directory = os.path.dirname(now_directory) + '/'
    read_filename = root_directory + 'dataset/all_news.txt'
    write_filename = root_directory + 'dataset/all_news_tags.txt'
    get_sentiment_tags(read_filename, write_filename)
    print('Total time %f seconds' % (time.clock() - start))

if __name__ == '__main__':
    test_get_sentiment_tags()
    